package ua.edu.nuos.jeetraining2012.cms.util.template.material;

import java.util.Map;

/**
 * IndexTemplate.
 * <p/>
 * This class uses to show a list of available materials for selected course.
 *
 * @author Victor N. Ganichev victor.ganichev@gmail.com
 * @version 1.0 Date: 03.06.13 Time: 13:34
 * @since 1.0
 */
public class IndexTemplate extends DefaultMaterialTemplate {

    // Constructors.

    /**
     * Default constructor.
     * <p/>
     * Define name of the template.
     *
     */
    public IndexTemplate() {
        super();
        logger.debug("IndexTemplate invocation.");
        templateName = "/view/material/index.ftl";
        logger.debug("redefine templateName to {}", templateName);
    }

    /**
     * Constructor with setting up course ID.
     * <p/>
     * <ul>
     *     <li>Predefine parent value map.</li>
     *     <li>Inject material service.</li>
     *     <li>Fill in values from service layer.</li>
     * </ul>
     *
     * @param courseId
     */
    public IndexTemplate(Long courseId) {
        this();
        logger.debug("IndexTemplate invocation with preset course ID {}", courseId);
        setCourseId(courseId);
        initValues();
    }

    // Getters & Setters.

    // Overrides
    @Override
    @SuppressWarnings("unchecked")
    public Map<String, Object> getMap() {
        logger.debug("getMap() - started.");
        Map<String, Object> map = super.getMap();
        map.put("courseId", courseId);
        map.put("courseTitle", courseTitle);
        map.put("courseStartDate", courseStartDate);
        map.put("courseFinishDate", courseFinishDate);
        map.put("syllabusLink", syllabusLink);
        map.put("materials_titles", materialTitles);
        map.put("courseDescription", courseDescription);
        map.put("courseLogo", courseLogo);
        map.put("courseShortDesc", courseShortDesc);
        map.put("hwList", hwList);
        map.put("isEnrolled", isEnrolled);
        map.put("isPending", isPending);

        Map<String, Object> i18nMap = (Map<String, Object>) map.get("i18n");
        map.put("i18n", getI18nMap(i18nMap));

        logger.debug("Filled map: {}", map);

        logger.debug("getMap() - finished.");
        return map;
    }

    /**
     * Summarised method to fill all necessary values.
     */
    private void initValues() {
        logger.debug("Trying to fetch values.");
        fetchCourseTitles();
        fetchCourseInfo();
        logger.debug("All possible values retrieved.");
    }


}
